package com.ruoyi.st.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.config.response.JsonResult;
import com.ruoyi.st.services.QRCodeService;
import io.lettuce.core.dynamic.annotation.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/st/qrCode")
public class QRCodeControlller {
    @Autowired
    private QRCodeService qrCodeService;
    @Autowired
    private ObjectMapper objectMapper;
//    @GetMapping("/generate")
//    public JsonResult getQrCode(@PathVariable String content, @PathVariable int width, @PathVariable int height){
//        qrCodeService.generateQRCodeImage(content, "qrCode.png", width, height);
//        return JsonResult.ok();
//    }

    // 新增接口，用于测试二维码生成
    @GetMapping("/generateTestQRCode")
    public JsonResult generateTestQRCode() {
        // 创建一个示例JSON数组
        List<Map<String, Object>> stAddrDetailList = new ArrayList<>();
        Map<String, Object> addrDetail1 = new HashMap<>();
        addrDetail1.put("userId", 1L);
        addrDetail1.put("address", "123 Main St");
        // 添加更多地址详情到列表中...
        stAddrDetailList.add(addrDetail1);

        // 将列表转换为JSON字符串
        String jsonContent;
        try {
            jsonContent = objectMapper.writeValueAsString(stAddrDetailList);
        } catch (JsonProcessingException e) {
            return JsonResult.ok("Failed to convert address list to JSON: " + e.getMessage());
        }

        // 生成二维码图片的路径
        String qrCodePath = "/qrcode/test_qrCode.png";

        try {
            // 生成包含示例JSON的二维码
            qrCodeService.generateQRCodeImage(jsonContent, qrCodePath, 300, 300);
            return JsonResult.ok("Test QR Code generated successfully");
        } catch (Exception e) {
            // 处理异常情况
            return JsonResult.ok("Failed to generate test QR Code: " + e.getMessage());
        }
    }
}
